package com.hxzy.course.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzy.course.mapper.ArrangeCourseMapper;
import com.hxzy.course.mapper.CourseMapper;
import com.hxzy.course.mapper.StaffMapper;
import com.hxzy.course.mapper.TimeMapper;
import com.hxzy.course.pojo.ArrangeCourse;
import com.hxzy.course.pojo.Course;
import com.hxzy.course.pojo.Staff;
import com.hxzy.course.pojo.Time;
import com.hxzy.region.dao.RegionMapper;
import com.hxzy.region.mapper.ClassRoomMapper;
import com.hxzy.region.pojo.ClassRoom;
import com.hxzy.region.pojo.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CourseService {

    @Autowired
    private ArrangeCourseMapper acm;
    @Autowired
    private ArrangeCourseMapper arrangeCourseMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TimeMapper timeMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private ClassRoomMapper classRoomMapper;

    public boolean addCourse(Course cou,String teacherId){
        int num = courseMapper.insert(cou);
        ArrangeCourse ac = new ArrangeCourse();
        ac.setCoId(cou.getCoId());
        Integer id = Integer.parseInt(teacherId);
        ac.setStaffId(id);
        int number = acm.insert(ac);
        return num > 0 && number > 0;
    }

    public List<Region> getAllRegion(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("t_region_flag","1");
        List<Region> list = regionMapper.selectList(null);
        return list;
    }

    public List<Course> queryAllCourse(){
        List<Course> list = courseMapper.selectList(null);
        return list;
    }

    public Staff queryTeacherByCourseId(Integer id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("co_id",id);
        queryWrapper.isNull("ti_id");
        queryWrapper.isNull("date");
        queryWrapper.eq("ac_flag","1");
        ArrangeCourse arrangeCourse = arrangeCourseMapper.selectOne(queryWrapper);
        Staff teacher = staffMapper.selectById(arrangeCourse.getStaffId());
        return teacher;
    }

    public Page<Map<String, Object>> getCourseInfo(Integer pageNum,Integer pageSize){
        //进行分页
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("co_type","团课");
        queryWrapper.eq("co_flag","1");
        Page<Course> page = new Page<Course>(pageNum,pageSize);
        List<Course> a = courseMapper.selectList(queryWrapper);
        System.out.println("----------------------------------------------");
        System.out.println("团课有这些");
        System.out.println(a);
        Page<Course> courses = courseMapper.selectPage(page,queryWrapper);
        List<Map<String,Object>> list = new ArrayList<>();
        for (Course course: courses.getRecords()) {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("co_id",course.getCoId());
            qw.isNull("ti_id");
            qw.isNull("date");
            qw.eq("ac_flag",1);
            ArrangeCourse arrangeCourse = arrangeCourseMapper.selectOne(qw);
            System.out.println("----------------------------------------------------------");
            System.out.println("对应的课表：" + arrangeCourse);
            if(arrangeCourse != null){
                Map<String,Object> map = new HashMap();
                map.put("course",course);
                Staff teacher = staffMapper.selectById(arrangeCourse.getStaffId());
                map.put("teacher",teacher);
                map.put("arrangeId",arrangeCourse.getArrangeId());
                list.add(map);
            }
        }
        Page<Map<String, Object>> realCourse = new Page<>(pageNum,pageSize);
        realCourse.setTotal(courses.getTotal());
        realCourse.setSize(courses.getSize());
        realCourse.setCurrent(courses.getCurrent());
        realCourse.setPages(courses.getPages());
        realCourse.setRecords(list);
        return realCourse;
    }

    public List<Course> queryTuanKe(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("co_type","团课");
        queryWrapper.eq("co_flag","1");
        List<Course> cou = courseMapper.selectList(queryWrapper);
        return cou;
    }

    public List<ClassRoom> queryClassRoom(String id){
        QueryWrapper queryWrapper = new QueryWrapper();
        int reId = Integer.valueOf(id);
        queryWrapper.eq("re_id",reId);
        queryWrapper.eq("cr_flag","1");
        List<ClassRoom> list = classRoomMapper.selectList(queryWrapper);
        return list;
    }

    public Page<Map<String, Object>> conditionQueryTuanke(String time,String location,String space,String teacher,String course,String duringTime,Integer pageNum,Integer pageSize){
        QueryWrapper<ArrangeCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("co_id","select co_id from course where co_type = '团课'");
        queryWrapper.isNotNull("ti_id");
        queryWrapper.isNotNull("date");
        if(!"0".equals(time)){
            queryWrapper.eq("ti_id",time);
        }
        if(!"0".equals(location)){
            queryWrapper.eq("t_region_id",location);
        }
        if(!"0".equals(space)){
            queryWrapper.eq("cr_id",space);
        }
        if(!"0".equals(teacher)){
            System.out.println("教师");
            queryWrapper.eq("staff_id",teacher);
        }
        if(!"0".equals(course)){
            System.out.println("课程");
            queryWrapper.eq("co_id",course);
        }
        String[] times = duringTime.trim().split("-");
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = sdf1.parse(times[0].trim());
            String starttime = sdf2.format(date1);
            System.out.println(starttime);
            Date date2 = sdf1.parse(times[1].trim());
            String finishtime = sdf2.format(date2);
            System.out.println(finishtime);
            queryWrapper.isNotNull("date");
            queryWrapper.isNotNull("ti_id");
            queryWrapper.between("date",starttime,finishtime);
            //保证课表状态在存在的
            queryWrapper.eq("ac_flag","1");
            //进行分页
            Page<ArrangeCourse> page = new Page<ArrangeCourse>(pageNum,pageSize);
            Page<ArrangeCourse> schedule = arrangeCourseMapper.selectPage(page,queryWrapper);
            List<Map<String,Object>> message = new ArrayList<>();
            for (ArrangeCourse arrangecourse:schedule.getRecords()) {
                Map<String,Object> map = new HashMap<>();
                Course cour = courseMapper.selectById(arrangecourse.getCoId());
                map.put("course",cour);
                Time ti = timeMapper.selectById(arrangecourse.getTiId());
                map.put("time",ti);
                Staff staff = staffMapper.selectById(arrangecourse.getStaffId());
                map.put("teacher",staff);
                Region region = regionMapper.selectById(arrangecourse.getTRegionId());
                map.put("location",region);
                ClassRoom classRoom = classRoomMapper.selectById(arrangecourse.getCrId());
                map.put("space",classRoom);
                map.put("arrangeId",arrangecourse.getArrangeId());
                map.put("date",arrangecourse.getDate());
                message.add(map);
            }
            Page<Map<String, Object>> realSchedule = new Page<>(pageNum,pageSize);
            realSchedule.setTotal(schedule.getTotal());
            realSchedule.setSize(schedule.getSize());
            realSchedule.setCurrent(schedule.getCurrent());
            realSchedule.setPages(schedule.getPages());
            realSchedule.setRecords(message);
            return realSchedule;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Page<Map<String, Object>> conditionQuerySijiao(String time,String location,String space,String teacher,String course,String duringTime,Integer pageNum,Integer pageSize){
        QueryWrapper<ArrangeCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("co_id","select co_id from course where co_type = '私教'");
        queryWrapper.isNotNull("ti_id");
        queryWrapper.isNotNull("date");
        if(!"0".equals(time)){
            queryWrapper.eq("ti_id",time);
        }
        if(!"0".equals(location)){
            queryWrapper.eq("t_region_id",location);
        }
        if(!"0".equals(space)){
            queryWrapper.eq("cr_id",space);
        }
        if(!"0".equals(teacher)){
            System.out.println("教师");
            queryWrapper.eq("staff_id",teacher);
        }
        if(!"0".equals(course)){
            System.out.println("课程");
            queryWrapper.eq("co_id",course);
        }
        String[] times = duringTime.trim().split("-");
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = sdf1.parse(times[0].trim());
            String starttime = sdf2.format(date1);
            System.out.println(starttime);
            Date date2 = sdf1.parse(times[1].trim());
            String finishtime = sdf2.format(date2);
            System.out.println(finishtime);
            queryWrapper.isNotNull("date");
            queryWrapper.isNotNull("ti_id");
            queryWrapper.between("date",starttime,finishtime);
            //保证课表状态在存在的
            queryWrapper.eq("ac_flag","1");
            //进行分页
            Page<ArrangeCourse> page = new Page<ArrangeCourse>(pageNum,pageSize);
            Page<ArrangeCourse> schedule = arrangeCourseMapper.selectPage(page,queryWrapper);
            List<Map<String,Object>> message = new ArrayList<>();
            for (ArrangeCourse arrangecourse:schedule.getRecords()) {
                Map<String,Object> map = new HashMap<>();
                Course cour = courseMapper.selectById(arrangecourse.getCoId());
                map.put("course",cour);
                Time ti = timeMapper.selectById(arrangecourse.getTiId());
                map.put("time",ti);
                Staff staff = staffMapper.selectById(arrangecourse.getStaffId());
                map.put("teacher",staff);
                Region region = regionMapper.selectById(arrangecourse.getTRegionId());
                map.put("location",region);
                ClassRoom classRoom = classRoomMapper.selectById(arrangecourse.getCrId());
                map.put("space",classRoom);
                map.put("arrangeId",arrangecourse.getArrangeId());
                map.put("date",arrangecourse.getDate());
                message.add(map);
            }
            Page<Map<String, Object>> realSchedule = new Page<>(pageNum,pageSize);
            realSchedule.setTotal(schedule.getTotal());
            realSchedule.setSize(schedule.getSize());
            realSchedule.setCurrent(schedule.getCurrent());
            realSchedule.setPages(schedule.getPages());
            realSchedule.setRecords(message);
            return realSchedule;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean deleteSchedule(String delFlag,String delId){
        ArrangeCourse arrangeCourse = arrangeCourseMapper.selectById(delId);
        arrangeCourse.setAcFlag(0);
        int n = arrangeCourseMapper.updateById(arrangeCourse);
        Course course = new Course();
        course.setCoId(arrangeCourse.getCoId());
        course.setCoFlag(0);
        int m = courseMapper.updateById(course);
        return n > 0 && m > 0;
    }

    public Integer getCourseIdByCourseName(String co_name){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("co_name",co_name);
        List<Course> course = courseMapper.selectList(queryWrapper);
        return course.get(0).getCoId();
    }

    public List<Map<String, Object>> getOrderCourseInfo(String co_name){
        Integer co_id = this.getCourseIdByCourseName(co_name);
        QueryWrapper<ArrangeCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("ti_id");
        queryWrapper.isNotNull("date");
        queryWrapper.eq("co_id",co_id);
        queryWrapper.eq("ac_flag","1");
        List<ArrangeCourse> list = arrangeCourseMapper.selectList(queryWrapper);
        List<Map<String,Object>> message = new ArrayList<>();
        for (ArrangeCourse arrangecourse:list) {
            Map<String,Object> map = new HashMap<>();
            Course cour = courseMapper.selectById(arrangecourse.getCoId());
            map.put("course",cour);
            Time ti = timeMapper.selectById(arrangecourse.getTiId());
            map.put("time",ti);
            Staff staff = staffMapper.selectById(arrangecourse.getStaffId());
            map.put("teacher",staff);
            Region region = regionMapper.selectById(arrangecourse.getTRegionId());
            map.put("location",region);
            ClassRoom classRoom = classRoomMapper.selectById(arrangecourse.getCrId());
            map.put("space",classRoom);
            map.put("arrangeId",arrangecourse.getArrangeId());
            map.put("date",arrangecourse.getDate());
            message.add(map);
        }
        System.out.println("查到的信息为：");
        System.out.println(message);
        return message;
    }
}
